package dev.ece.suit.loader.models.ms3d;

import java.io.IOException;

import dev.ece.util.io.LittleEndianInputStream;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector3f;

/**
 * 三角面
 * @author 飘渺青衣
 * <b>E-mail：</b>3375758@qq.com
 */
public class MS3DTriangle {

	private int[] indice = new int[3];
	
	private Vector3f[] normals = new Vector3f[3];
	
	private Vector2f[] uvs = new Vector2f[3];
	
	private MS3DTriangle() {}
	
	public static MS3DTriangle[] load(LittleEndianInputStream is) throws IOException {
		MS3DTriangle[] triangles = new MS3DTriangle[is.readUnsignedShort()];
		MS3DTriangle triangle = null;
		for(int i=0; i<triangles.length; i++) {
			triangles[i] = triangle = new MS3DTriangle();
			is.skip(2);
			for(int j=0; j<3; j++) {
				triangle.indice[j] = is.readUnsignedShort();
			}
			for(int j=0; j<3; j++) {
				triangle.normals[j] = new Vector3f(
						is.readFloat(), is.readFloat(), is.readFloat());
			}
			for(int j=0; j<3; j++) {
				triangle.uvs[j] = new Vector2f();
				triangle.uvs[j].setX(is.readFloat());
			}
			for(int j=0; j<3; j++) {
				triangle.uvs[j].setY(is.readFloat());
			}
			is.skip(2);
		}
		return triangles;
	}
	
	public int[] getIndice() {
		return indice;
	}

	public Vector3f[] getNormals() {
		return normals;
	}

	public Vector2f[] getUvs() {
		return uvs;
	}

}
